package main

import "fmt"

func main() {
	arr := []int{3, 2, 1, 10, 5}
	fmt.Println("小到大============")
	var small2LargeArr []int = make([]int, len(arr))
	copy(small2LargeArr, arr)
	small2Large(small2LargeArr)
	fmt.Println("大到小================")
	var large2SmallArr []int = make([]int, len(arr))
	copy(large2SmallArr, arr)
	large2Small(large2SmallArr)
}

func small2Large(arr []int) {
	count := 0
	fmt.Println("排序前")
	fmt.Println(arr)
	fmt.Println("排序中")
	for i := 0; i < len(arr)-1; i++ {
		jUp := len(arr) - 1 - i
		for j := 0; j < jUp; j++ {
			count++
			if arr[j] > arr[j+1] {
				t := arr[j]
				arr[j] = arr[j+1]
				arr[j+1] = t
			}
		}
		fmt.Println(arr)
	}
	fmt.Println("排序后:")
	fmt.Println(arr)
	fmt.Printf("执行了 %d 次判断 \n", count)
}

func large2Small(arr []int) {

	count := 0
	fmt.Println("排序前")
	fmt.Println(arr)
	fmt.Println("排序中")
	for i := len(arr) - 1; i > 0; i-- {
		jDown := len(arr) - 1 - i
		for j := len(arr) - 1; j > jDown; j-- {
			count++
			if arr[j] > arr[j-1] {
				t := arr[j]
				arr[j] = arr[j-1]
				arr[j-1] = t
			}
		}
		fmt.Println(arr)
	}
	fmt.Println("排序后:")
	fmt.Println(arr)
	fmt.Printf("执行了 %d 次判断 \n", count)

}
